/*
 * @Author: 陈德基
 * @Date: 2021-11-15 18:48:47
 * @LastEditTime: 2021-11-28 09:59:07
 * @Description: ;
 * @FilePath: \copy_gsy_github_app_flutter\lib\page\login\widgets\login.dart
 * 联系方式：
 * wx: melody_2009
 * qq: 7815171323 
 */
import 'package:copy_gsy_github_app_flutter/common/net/address.dart';
import 'package:copy_gsy_github_app_flutter/common/style/gsy_style.dart';
import 'package:copy_gsy_github_app_flutter/page/login/widgets/animated_background.dart';
import 'package:copy_gsy_github_app_flutter/page/login/widgets/login_web_view.dart';
import 'package:copy_gsy_github_app_flutter/page/login/widgets/particle/particle_widget.dart';
import 'package:copy_gsy_github_app_flutter/routes/app_pages.dart';
import 'package:copy_gsy_github_app_flutter/widget/app_flex_button.dart';
import 'package:copy_gsy_github_app_flutter/widget/app_input_widget.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';

import '../index.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

/// hello
class HelloWidget extends GetView<LoginController> {
  const HelloWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final TextEditingController userController = new TextEditingController();
    final TextEditingController pwController = new TextEditingController();
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: () {
        FocusScope.of(context).requestFocus(FocusNode());
      },
      child: Scaffold(
        body: Container(
          color: Get.theme.primaryColor,
          child: Stack(
            children: [
              Positioned.fill(child: AnimatedBackground()),
              Positioned.fill(child: ParticlesWidget(30)),
              Center(
                child: SafeArea(
                    child: SingleChildScrollView(
                  child: Card(
                    elevation: 5.0,
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.all(Radius.circular(10))),
                    color: APPColors.cardWhite,
                    margin: EdgeInsets.only(
                      left: 30.w,
                      right: 30.w,
                    ),
                    child: Padding(
                      padding: EdgeInsets.only(
                          left: 30.0, top: 40.0, right: 30.0, bottom: 0.0),
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Image(
                              image: new AssetImage(AppICons.DEFAULT_USER_ICON),
                              width: 90.0.w,
                              height: 90.0.w),
                          Padding(padding: new EdgeInsets.all(10.0)),
                          AppInputWidget(
                            hintText: 'login_username_hint_text'.tr,
                            iconData: AppICons.LOGIN_USER,
                            onChanged: (String value) {
                              controller.state.name = value;
                            },
                            controller: userController,
                          ),
                          Padding(padding: new EdgeInsets.all(10.0)),
                          AppInputWidget(
                            hintText: 'login_password_hint_text'.tr,
                            iconData: AppICons.LOGIN_PW,
                            obscureText: true,
                            onChanged: (String value) {
                              controller.state.psw = value;
                            },
                            controller: pwController,
                          ),
                          Container(
                            height: 50,
                            child: Row(
                              children: <Widget>[
                                new Expanded(
                                  child: new AppFlexButton(
                                    text: 'login_text'.tr,
                                    color: Theme.of(context).primaryColor,
                                    textColor: APPColors.textWhite,
                                    fontSize: 16,
                                    onPress: loginIn,
                                  ),
                                ),
                                new SizedBox(
                                  width: 10,
                                ),
                                new Expanded(
                                  child: new AppFlexButton(
                                    text: '安全登录',
                                    color: Theme.of(context).primaryColor,
                                    textColor: APPColors.textWhite,
                                    fontSize: 16,
                                    onPress: oauthLogin,
                                  ),
                                ),
                              ],
                            ),
                          ),
                          new Padding(padding: new EdgeInsets.all(15.0)),
                          InkWell(
                            onTap: () {
                              // CommonUtils.showLanguageDialog(context);
                            },
                            child: Text(
                              'switch_language'.tr,
                              style: TextStyle(color: APPColors.subTextColor),
                            ),
                          ),
                          new Padding(padding: new EdgeInsets.all(15.0)),
                        ],
                      ),
                    ),
                  ),
                )),
              )
            ],
          ),
        ),
      ),
    );
  }

  /// 原版作者将账号登录的功能标注过时
  @deprecated
  loginIn() async {
    Fluttertoast.showToast(
        msg: 'Login_deprecated'.tr,
        gravity: ToastGravity.CENTER,
        toastLength: Toast.LENGTH_LONG);
    return;
  }

  oauthLogin() async {
    /// 进入到网页登录，在网页授权成功之后，将授权码返回
    ///
    String? code =
        await Get.to(LoginWebView(Address.getOAuthUrl(), 'oauth_text'.tr));
    // String? code = await NavigatorUtils.goLoginWebView(context,
    //     Address.getOAuthUrl(), "${GSYLocalizations.i18n(context)!.oauth_text}");

    if (code != null && code.length > 0) {
      ///通过 redux 去执行登陆流程
      // StoreProvider.of<GSYState>(context).dispatch(OAuthAction(context, code));
      var result = await controller.login(code);
      if (result != null) {
        Get.offAll(AppRoute.HOME);
      } else {
        Fluttertoast.showToast(msg: 'login_fail'.tr);
      }
    }
  }
}
